<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>at</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Double-ended Queues</a> &gt; <a href=
    "at.html">at</a>
  </div>

  <div class="name-format">
    at
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;deque&gt;
  <a href=
"../containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc );
  const <a href=
"../containers.html">TYPE</a>&amp; at( <strong>size_type</strong> loc ) const;
</pre>

  <p>The at() function returns a reference to the element in the
  dequeue at index <em>loc</em>. The at() function is safer than the []
  operator, because it won&#39;t let you reference items outside the
  bounds of the dequeue.</p>

  <p>For example, consider the following code:</p>
  <pre class="example-code">
 vector&lt;int&gt; v( 5, 1 );
 for( int i = 0; i &lt; 10; i++ ) {
   cout &lt;&lt; &quot;Element &quot; &lt;&lt; i &lt;&lt; &quot; is &quot; &lt;&lt; v[i] &lt;&lt; endl;
 }              
</pre>

  <p>This code overrunns the end of the vector, producing potentially
  dangerous results. The following code would be much safer:</p>
  <pre class="example-code">
 vector&lt;int&gt; v( 5, 1 );
 for( int i = 0; i &lt; 10; i++ ) {
   cout &lt;&lt; &quot;Element &quot; &lt;&lt; i &lt;&lt; &quot; is &quot; &lt;&lt; v.at(i) &lt;&lt; endl;
 }              
</pre>

  <p>Instead of attempting to read garbage values from memory, the at()
  function will realize that it is about to overrun the vector and will
  throw an exception.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    (C++ Multimaps) <a href=
    "../cppmultimap/multimap_operators.html">Multimap
    operators</a><br>
    <a href="container_operators.html">Deque operators</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
